package com.atguigu.gmall.common.util;

import com.atguigu.gmall.common.constant.RedisConst;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lhh
 * @ClassName UserAuthUtil
 * @date 2022 2022/12/18
 */

public class UserAuthUtil {

    public static HttpServletRequest request(){
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest();
    }

    public static Long getUserId(){
        HttpServletRequest request = request();
        String userId = request.getHeader(RedisConst.USER_ID_HEADER);
        if (StringUtils.isEmpty(userId)){
            return null;
        }
        return Long.parseLong(userId);
    }
}
